//
//  UserInfoManager.m
//  MechElecProject
//
//  Created by mac on 2017/5/2.
//  Copyright © 2017年 tianEr. All rights reserved.
//

#import "UserInfoManager.h"
#import "ViewController.h"

#define USERLOGINDARA @"Supermarket_UserInfo"

@implementation UserInfoManager

+ (instancetype)sharedManager {
    static UserInfoManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc]init];
    });
    return manager;
}

- (id)init {
    if (self = [super init]) {
        if ([self  gitUserDefaultsData]) {
            [self setValuesForKeysWithDictionary:[self gitUserDefaultsData]];
            [self writeToLoal:[self gitUserDefaultsData]];
        }else {
            [self removeUserInfoData];
        }
    }
    return self;
}

//登陆成功换成数据
- (void)saveUserInfo:(id)data {
    [self setValuesForKeysWithDictionary:data];
    [self writeToLoal:data];
}

/** 清除本地数据以及缓存数据*/
- (void)removeUserInfoData {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:USERLOGINDARA];
 
    self.id = @"";
    self.name = @"";
    self.mobile = @"";
    self.id_code = @"";
    self.point = @"";
    self.amount_point = @"";
}

//登陆成功吧数据存入本地
- (void)writeToLoal:(id)data{
    NSUserDefaults * userDefaults =[NSUserDefaults standardUserDefaults];
    [userDefaults setObject:data forKey:USERLOGINDARA];
    [userDefaults synchronize];
}

/** 是否登陆过*/
- (BOOL)isLogin{
    id data = [self gitUserDefaultsData];
    if ([data count] > 0) {
        return YES;
    }
    return NO;
}

/** 去登陆*/
- (void)goLoginPrompt:(BOOL)prompt{
    
}

- (id)gitUserDefaultsData{
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    id data = [userDefaults objectForKey:USERLOGINDARA];
    return data;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

- (void)goLogin {
    ViewController *login = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:login];
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    window.backgroundColor = [UIColor whiteColor];
    window.rootViewController = nav;
    [window makeKeyAndVisible];
}

@end
